//
//  RegisterViewController.m
//  PlaneWar2
//
//  Created by HappyMilk 02 on 2017/4/7.
//  Copyright © 2017年 HappyMilk 02. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginHeader.h"

@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *username_TF;
@property (weak, nonatomic) IBOutlet UITextField *password_TF;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)popvc:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)registerAction:(id)sender {
    NSString *username = _username_TF.text;
    NSString *password = _password_TF.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = LOC_STR(@"Please fill in the correct account or password! Account password must be less than 6!");
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    SHOW_HUD(@"Is registering...");
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = username;// 设置用户名
    user.password =  password;// 设置密码
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        HIDE_HUD
        if (succeeded) {
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"" message:LOC_STR(@"Registration success!please sign in") delegate:nil cancelButtonTitle:LOC_STR(@"Confirm") otherButtonTitles:nil, nil];
            [alertview show];
            
            //[self.scrollview setContentOffset:(CGPoint){0,0} animated:YES];
            [self dismissViewControllerAnimated:YES completion:nil];
            //[self.navigationController popViewControllerAnimated:true];
        } else {
            SHOW_HUD_MESSAGE(@"Registration failed, user name may exist, please try again or return to login");
        }
        
        NSLog(@"%@",error);
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.username_TF resignFirstResponder];
    [self.password_TF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
